/**
 * 
 */
package ar.com.sebasoft.prestalibro.util.impl;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.util.Log;
import ar.com.sebasoft.prestalibro.util.EncryptionStrategy;
import ar.com.sebasoft.prestalibro.util.SystemHelper;

/**
 * @author SebaSOFT
 * 
 */
public class EncryptionMD5Strategy implements EncryptionStrategy {

	@Override
	public String encryptKey(String clave) {
		String res = "";

		try {
			// Create MD5 Hash
			MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
			digest.update(clave.getBytes());
			byte messageDigest[] = digest.digest();

			// Create Hex String
			StringBuilder hexString = new StringBuilder();
			for (byte aMessageDigest : messageDigest) {
				String h = Integer.toHexString(0xFF & aMessageDigest);
				while (h.length() < 2) {
					h = "0" + h;
				}
				hexString.append(h);
			}
			return hexString.toString();
		} catch (NoSuchAlgorithmException e) {
			Log.e(SystemHelper.APP_TAG, "Error when MD5 hasing the password", e);
		}

		return res;
	}

}
